﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Admin_Tintuc : System.Web.UI.Page
{
    string strid = "";
    BUS_Category objC = new BUS_Category();
    BUS_News objP = new BUS_News();
    protected void Page_Load(object sender, EventArgs e)
    {
        Page.Title = "Quản Lý Tin Tức";
        if (!IsPostBack)
        {
            if (Session["AdminLogin"] != null)
            {
                if (Request.QueryString.Get("cid") == null)
                {
                    var dataC = objC.Category_getMinCgid();
                    int cid = int.Parse(dataC.Rows[0]["cgid"].ToString());
                    HienLienKet(cid);
                    HienThiSanPham(cid);
                }
                else
                {
                    HienLienKet(Convert.ToInt16(Request.QueryString.Get("cid")));
                    HienThiSanPham(Convert.ToInt16(Request.QueryString.Get("cid")));
                }
            }
            else Response.Redirect("Dangnhap.aspx");
        }
    }
    public string CheckTrangThai(object obj)
    {
        string b = obj.ToString();
        if (b == "1")
            return "Mở";
        else
            return "Đóng";
    }
    public void HienLienKet(int id)
    {
        try
        {
            var label = objC.Category_getById(id);
            lienKet.Text = label.Rows[0]["Name"].ToString();
            linkQLTT.NavigateUrl = "Tintuc.aspx?cid=" + id;
            linkthemtt.NavigateUrl = "Themtintuc.aspx";
        }
        catch
        {
        }

    }
    public void HienThiSanPham(int Cgid)
    {
        try
        {
            gvSanpham.DataSource = objP.News_getAllByCgid(Cgid);
            gvSanpham.DataBind();
        }
        catch (Exception)
        {

            //throw;
        }
    }

    protected void gvSanpham_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvSanpham.PageIndex = e.NewPageIndex;
        if (Request.QueryString.Get("cid") == null)
        {
            var dataC = objC.Category_getMinCgid();
            int cid = int.Parse(dataC.Rows[0]["cgid"].ToString());
            HienLienKet(cid);
            HienThiSanPham(cid);
        }
        else
        {
            HienLienKet(Convert.ToInt16(Request.QueryString.Get("cid")));
            HienThiSanPham(Convert.ToInt16(Request.QueryString.Get("cid")));
        }
    }
    protected void gvSpanpham_OnRowDataBound(object sender, GridViewRowEventArgs e)
    {
        try
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Attributes.Add("style", "background-color: #fff ; color: #000;");
                e.Row.Attributes.Add("onmouseover", "style.backgroundColor='#ccc'");
                e.Row.Attributes.Add("onmouseout", "style.backgroundColor='#fff'");
            }
        }
        catch { }
    }

    protected void btnxoasp_Click(object sender, EventArgs e)
    {
        //BUS_OrderDetail objO = new BUS_OrderDetail();
        for (int i = 0; i < gvSanpham.Rows.Count; i++)
        {
            CheckBox chk = (CheckBox)gvSanpham.Rows[i].Cells[0].FindControl("chkChon");
            if (chk.Checked)
            {
                int id = int.Parse(gvSanpham.Rows[i].Cells[1].Text);
                //lap danh sach xoa
                //objO.OrderDetail_addtodel(id);
                objP.News_addtodel(id);


            }
        }
        //tien hanh xoa
        if (objP.News_del())
        {
            for (int i = 0; i < gvSanpham.Rows.Count; i++)
            {
                CheckBox chk = (CheckBox)gvSanpham.Rows[i].Cells[0].FindControl("chkChon");
                if (chk.Checked)
                {
                    int id = int.Parse(gvSanpham.Rows[i].Cells[1].Text);
                    //lap danh sach xoa
                    objP.News_addtodel(id);
                }
            }
            if (objP.News_del())
            {
                if (Request.QueryString.Get("cid") == null)
                {
                    var dataC = objC.Category_getMinCgid();
                    int cid = int.Parse(dataC.Rows[0]["cgid"].ToString());
                    HienLienKet(cid);
                    HienThiSanPham(cid);
                }
                else
                {
                    HienLienKet(Convert.ToInt16(Request.QueryString.Get("cid")));
                    HienThiSanPham(Convert.ToInt16(Request.QueryString.Get("cid")));
                }
            }
            else
            {
                Response.Write("<script>alert(\"Xóa không thành công <..*!>\")</script>");
            }
        }
        else Response.Write("<script>alert(\"Xóa không thành công <..*!>\")</script>");

    }
}